package com.cat.binaryTree;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/binary-tree-coloring-game/description/
 * @create 2025/9/13 16:07
 * @since JDK17
 */

public class Solution28 {
    int x, num;
    TreeNode a;
    void find(TreeNode root) {
        if (root == null) {
            return ;
        }
        if (root.val == x) {
            a = root;
            return;
        }
        find(root.left);
        find(root.right);
    }
    int f(TreeNode root) {
        if (root == null) {
            return 0;
        }
        return 1 + f(root.left) + f(root.right);
    }



    public boolean btreeGameWinningMove(TreeNode root, int n, int x) {
        this.x = x;
        find(root);
        if (root != a) {
            num = f(a);
        } else {
            num = Math.min(f(root.left), f(root.right)) + 1;
        }
        int num1 = f(a), num2 = n - num1;
        if (num2 > num1) {
            return true;
        }
        num1 = f(root.left);   //
        num2 = f(root.right);
        if (num1 > num2) {  // num2是大的
            int t = num1;
            num1 = num2;
            num2 = t;
        }
        return num2 > n - num2;
    }
}
